package cn.yuanqiao.web.controller.archive.detection;

import java.io.*;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.yuanqiao.common.core.context.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import cn.yuanqiao.archive.detection.domain.InspectionRecords;
import cn.yuanqiao.archive.detection.service.IInspectionRecordsService;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 四性检测记录Controller
 *
 * @author xy
 * @date 2023-08-07
 */
@RestController
@RequestMapping("/archive/inspectionRecords")
public class InspectionRecordsController extends BaseController
{
    @Autowired
    private IInspectionRecordsService inspectionRecordsService;

    /**
     * 查询四性检测记录列表
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:InspectionRecords:list')")
*/
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('archive:fourNatures:detectionRecords:list')")
    public TableDataInfo list(InspectionRecords inspectionRecords)
    {
        startPage();
        List<InspectionRecords> list = inspectionRecordsService.selectInspectionRecordsList(inspectionRecords);
        return getDataTable(list);
    }

    /**
     * 导出四性检测记录列表
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:InspectionRecords:export')")
*/
    @Log(title = "四性检测记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionRecords inspectionRecords)
    {
        List<InspectionRecords> list = inspectionRecordsService.selectInspectionRecordsList(inspectionRecords);
        ExcelUtil<InspectionRecords> util = new ExcelUtil<InspectionRecords>(InspectionRecords.class);
        util.exportExcel(response, list, "四性检测记录数据");
    }

    /**
     * 获取四性检测记录详细信息
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:InspectionRecords:query')")
*/
    @GetMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPermi('archive:fourNatures:detectionRecords:detail')")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(inspectionRecordsService.selectInspectionRecordsById(id));
    }

    /**
     * 新增四性检测记录
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:InspectionRecords:add')")
*/
    @Log(title = "四性检测记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionRecords inspectionRecords)
    {
        return toAjax(inspectionRecordsService.insertInspectionRecords(inspectionRecords));
    }

    /**
     * 修改四性检测记录
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:InspectionRecords:edit')")
*/
    @PreAuthorize("@ss.hasPermi('archive:fourNatures:detectionRecords:edit')")
    @Log(title = "四性检测记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionRecords inspectionRecords)
    {
        return toAjax(inspectionRecordsService.updateInspectionRecords(inspectionRecords));
    }

    /**
     * 删除四性检测记录
     */
/*
    @PreAuthorize("@ss.hasPermi('archive:InspectionRecords:remove')")
*/
    @PreAuthorize("@ss.hasPermi('archive:fourNatures:detectionRecords:delete')")
    @Log(title = "四性检测记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(inspectionRecordsService.deleteInspectionRecordsByIds(ids));
    }

    //导入，导出
    @Log(title = "四性检测记录", businessType = BusinessType.EXPORT)
    @PostMapping("/xmlExport")
    @PreAuthorize("@ss.hasPermi('archive:fourNatures:detectionRecords:export')")
    public void zipExport(HttpServletResponse response, InspectionRecords inspectionRecords) throws IOException {

         String path=inspectionRecordsService.zipExport(inspectionRecords);
         //将文件下载下来
        response.reset(); // 非常重要
        // 设置 CORS 响应头
        ResponseUtils.defaultResponse(response);
        //在线预览，浏览器支持的文件类型都能打开
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "inline; filename=" + new Date().getTime()+".xml");
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File(path)));
        byte[] buf = new byte[1024];
        int len = 0;
        OutputStream out = response.getOutputStream();
        while ((len = br.read(buf)) > 0){
            out.write(buf, 0, len);
        }
        System.gc();
        br.close();
        out.close();

    }
    //导入，导出
    @Log(title = "四性检测记录", businessType = BusinessType.IMPORT)
    @PostMapping("/xmlImportData")
    @PreAuthorize("@ss.hasPermi('archive:fourNatures:detectionRecords:add')")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
      boolean flag= inspectionRecordsService.xmlImportData(file);
      if(flag){
          return AjaxResult.success();
      }else{
          return AjaxResult.error();
      }

    }


}
